import request from '@/utils/request'

/**
 * 缴费查询接口
 */
export default {
  expire_days: 7,
  templates: null,
  // 获取短信信息(index-- 第几条模板 params-- 相关参数)
  getMessage(index, params) {
    if (params === null || index < 0 ||
          index > this.templates.length || params.length === 0) {
      return null
    }
    if (this.getMessageTemplateParamsLength(index) !== params.length) {
      return null
    }
    console.log(this.templates)
    var template = this.templates[index].templateContent
    for (var i = 1; i < params.length + 1; i++) {
      template = template.replace('{' + i + '}', params[i - 1])
    }
    return template
  },
  // 设置短信模板（第一步）
  setMessageTemplates(list) {
    this.templates = list
  },
  // 获取短信模板
  getMessageTemplate(index) {
    if (index < 0 || index > this.templates.lenggth) {
      return null
    }
    return this.templates[index]
  },
  getMessageTemplateParamsLength(index) {
    if (index < 0 || index > this.templates.lenggth) {
      return null
    }
    var template = this.templates[index].templateContent
    template = template.replace(/{/g, '')
    var count = 0
    var strArr = template.split(/}/g)
    for (var i = 0; i < strArr.length; i++) {
      if (strArr[i] !== '') {
        count++
      }
    }
    return count - 1
  },
  sendNotifyMsg(data) {
    return request({
      method: 'post',
      url: '/sendMsg/sendMessage',
      params: data
    })
  },
  searchMessageInfoList(data) {
    return request({
      method: 'get',
      url: '/msg/queryMsgList',
      params: data
    })
  },
  saveMessageInfo(data) {
    return request({
      method: 'post',
      url: '/msg/saveMsg',
      params: data
    })
  },
  updateMessageInfo(data) {
    return request({
      method: 'post',
      url: '/msg/updateMsg',
      params: data
    })
  },
  deleteMessageInfo(data) {
    return request({
      method: 'post',
      url: '/msg/deleteMsg',
      params: data
    })
  }
}
